package com.xrj4j.car_news.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.xrj4j.car_news.R
import com.xrj4j.car_news.databinding.ItemBrandBinding
import com.xrj4j.car_news.model.BrandItem

class BrandListAdapter(ctx: Context, private var dataList: MutableList<BrandItem>) :
    RecyclerView.Adapter<BrandListAdapter.BrandHolder>() {

    private val inflater = LayoutInflater.from(ctx)

    class BrandHolder(itemView: View, val binding: ItemBrandBinding) : RecyclerView.ViewHolder(itemView)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BrandHolder {
        val binding: ItemBrandBinding =
            DataBindingUtil.inflate(inflater, R.layout.item_brand, parent, false)
        return BrandHolder(binding.root,binding)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: BrandHolder, position: Int) {
        val data = dataList[position]
        holder.binding.item = data
    }

    @SuppressLint("NotifyDataSetChanged")
    fun addDataList(newDataList: List<BrandItem>?) {
        if (newDataList != null) {
            dataList.clear()
            dataList.addAll(newDataList)
            notifyDataSetChanged()
        }
    }

}